package cn.itcast.service;

import cn.itcast.interfaces.ReportService;
import cn.itcast.mapper.MemberMapper;
import cn.itcast.mapper.OrderMapper;
import cn.itcast.mapper.SetmealMapper;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: peterChen
 * @date: 2019/8/17 16:00
 * @description: TODO
 * @version: 1.0
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * data
     *     {
     *     reportDate:  ,
     *      todayNewMember :0,  今日新增会员数量
     *      totalMember :0,     总会员数
     *      thisWeekNewMember :0,   本周新增会员数
     *      thisMonthNewMember :0,  本月新增会员数
     *      todayOrderNumber :0,    今日预约数量
     *      todayVisitsNumber :0,   今日到诊数
     *      thisWeekOrderNumber :0, 本周预约数
     *      thisWeekVisitsNumber :0,    本周到诊数
     *      thisMonthOrderNumber :0,    本月预约数
     *      thisMonthVisitsNumber :0,   本月到诊数
     *     hotSetmeal :[
     *               {name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
     *               {name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
     *                     ]
     *     }
     */
    /**
     * 获取前台excel表格数据展示
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getBusinessReport() throws Exception {
        Map<String, Object> map = new HashMap<>();
        //今日日期
        String reportDate = DateUtils.parseDate2String(DateUtils.getToday());
        //获取本周第一天日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本月的第一天日期
        String firstDay4ThisMonth =DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //获取今日新增会员数
        Integer todayNewMember = memberMapper.findMemberCountByDate(reportDate);
        //获取总会员数
        Integer totalMember = memberMapper.findMemberTotalCount();
        //获取本周新增会员数
        Integer thisWeekNewMember = memberMapper.findMemberCountAfterDate(thisWeekMonday);
        //获取本月新增会员数
        Integer thisMonthNewMember=memberMapper.findMemberCountAfterDate(firstDay4ThisMonth);
        //获取今日预约数量
        Integer todayOrderNumber = orderMapper.findOrderCountByDate(reportDate);
        //获取今日到诊数量
        Integer todayVisitsNumber = orderMapper.findVisitCountByDate(reportDate);
        //获取本周预约数
        Integer thisWeekOrderNumber=orderMapper.findOrderCountAfterDate(thisWeekMonday);
        //获取本周到诊数
        Integer thisWeekVisitsNumber=orderMapper.findVisitCountAfterDate(thisWeekMonday);
        //获取本月预约数
        Integer thisMonthOrderNumber=orderMapper.findOrderCountAfterDate(firstDay4ThisMonth);
        //获取本月到诊数
        Integer thisMonthVisitsNumber=orderMapper.findVisitCountAfterDate(firstDay4ThisMonth);
        //获取套餐最火热的前四个套餐
        List<Map<String,Object>> hotSetmeal = orderMapper.findHotSetmeal();

        map.put("reportDate",reportDate);
        map.put("todayNewMember",todayNewMember);
        map.put("totalMember",totalMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("hotSetmeal",hotSetmeal);

        return map;
    }
}
